/**
 * Created by duandazhi on 2016/12/19.
 * 订单列表
 */

$(document).ready(function () {
    //查询所有的快递公司
    $.ajax({
        url: root + 'cms/order/expresscmps',
        type: 'post',
        dataType: 'json',
        success: function (data) {
            console.log(data);
            if (data.code == 1) {
                //填充数据
                var contentHtml = $("#expressCompanyContentTmple").render(data, {mydata: data});
                $("#expressCompany").html("<option value=''>请选择</option>" + contentHtml);
            } else {
                myAlert(data.message);
            }
        }
    });

    //查看详情
    $('#lookSome').click(function () {
        var updateArray = [];
        var typeArray = [];
        $(".ckSelect").each(function () {
            var check = $(this).is(":checked");
            if (check) {
                var _id = $(this).attr("ckId");
                updateArray.push(_id);
                typeArray.push($(this).attr("ckType"));
            }
        });
        if (updateArray.length == 0) {
            myAlert("请选择要查看的项");
            return;
        }
        if (updateArray.length > 1) {
            myAlert("每次只能查看一项");
            return;
        }
        $.ajax({
            url: root + 'cms/order/findOrderByIdDetail',
            type: 'post',
            dataType: 'json',
            data: {
                modelId: updateArray[0],
                orderType: typeArray[0]
            },
            success: function (data) {
                console.log(data);
                if (data.code == 1) {
                    //填充数据
                    var contentHtml = $("#detailContentTmple").render(data, {mydata: data});
                    $("#detail_content").html(contentHtml);
                    $('#orderDetailModal').modal();
                } else {
                    myAlert(data.message);
                }
            }
        });
    });
    //发货
    $('#addGoods').click(function () {
        var goodsIds = [];
        var orderType;
        var error = true;
        $(".ckSelect").each(function () {
            var check = $(this).is(':checked');
            if (check) {
                orderType = parseInt($(this).attr("ckType"));
                if (parseInt($(this).attr("ckOrderStatus")) != 1) {
                    error = false;
                    return;
                }
                var _id = $(this).attr("ckId");
                goodsIds.push(_id);
            }
        });
        if (!error) {
            myAlert("只能派发已支付的商品");
            return;
        }
        if (goodsIds.length == 0) {
            myAlert("请选择要派发的商品");
            return;
        }

        if (goodsIds.length > 1) {
            myAlert("只能选择一项进行发货");
            return;
        }
        if (orderType == 64) {
            myAlert("只有护理培训|培训义工可用");
            return;
        }
        goodsIds1 = goodsIds;
        $("#sentGoods").modal();
    });
    //查看运单
    $('#lookSomes').click(function () {
        var goodsIds = [];
        var error = true;
        $(".ckSelect").each(function () {
            var check = $(this).is(':checked');
            if (check) {
                if ($(this).attr("ckType") != 2 || $(this).attr("ckOrderStatus") <= 1 || $(this).attr("ckType") == 6) {
                    error = false;
                    return;
                }
                goodsIds.push($(this).attr("ckId"));
            }
        });
        if (!error) {
            myAlert("只能查看已发货的商城订单");
            return;
        }
        if (goodsIds.length == 0) {
            myAlert("请选择要派发的商品");
            return;
        }
        if (goodsIds.length > 1) {
            myAlert("派发多个商品必须是同一个订单号");
            return;
        }
        var url = goodsIds.join(",");
        var urladdress = root + "page/Wan/expressbill.html?id=" + url;
        window.open(urladdress, '', 'height=580, width=380, top=0, left=500, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');
    });
    //查看运单--->确认
    $("#sent").click(function () {

        var modelIds = goodsIds1.join(",");
        $('#sentvalidateForm').isValid(function (result) {

            if ($("#expressCompany").val().length == 0) {
                myAlert("请选择快递公司！");
                return;
            }
            if (!result) {
                myAlert("还有不符合规定的字段填写，请检查！");
                return;
            }
            $("#sent").addClass('disable_btn');

            $.ajax({
                url: root + 'cms/order/sendOrders',
                type: 'post',
                data: {
                    expressCompany: $("#expressCompany").val(),
                    ids: modelIds,
                    expressNumber: $("#expressNumber").val()
                }, success: function (data) {
                    if (data.code == 1) {
                        myAlert("发货成功");
                        $('#expressCompany').val('');
                        $('#expressNumber').val('');
                        $('#sentvalidateForm').validator("destroy");
                        goodsIds1.remove(0);
                        dipscontent(getfilter(), contentUrl);
                        $('#sentGoods').modal('hide');
                        $("#sent").removeClass('disable_btn');
                    } else {
                        myAlert("发货失败");
                        $("#sent").removeClass('disable_btn');
                    }
                }
            });
        });
    });
    //导出
    $(document).on('click', '#dataexport', function () {
        //获取数据长度
        var tr = $("#mycontent").find('tr').length;
        if (tr == 0) {
            myAlert("没有数据,不能导出！");
            return;
        } else {
            var delArray = [];
            $(".ckSelect").each(function () {
                var check = $(this).is(':checked');
                if (check) {
                    var _id = $(this).attr("ckId");
                    delArray.push(_id);
                }
            });
            if (delArray.length == 0) {
                myAlert("请选择你要导出的数据!");
            }
            //导出选中项
            else if (delArray.length > 0) {
                var modelIds = delArray.join(",");
                location.href = root + 'cms/order/fileDownload?modelIds=' + modelIds;
            }
        }
    });
});